<template>
  <div>
    <!--<TopFilter :list="partMonthFilterList" :cols="3" :labelWidth="90" @filterChange="changeHandle"></TopFilter>-->
    <TopFilter ref="searchOrdersPanelwax" :list="partMonthFilterList" :isSubmitBtn="true" :cols="4" @filterChange="changeHandle" @onCollapse="collapseHandle" />
    <FilterTable
      ref="table"
      columnsRef="monthlyReportwssii1yueb"
      :columns="columns"
      :isSelectColumn="false"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="arr => (this.onChangeColumn = arr)"
      :isExportExcel="true"
      :onCalcExportData="exportFun"
      exportFileName="配件入出存月报.xlsx"
      :datakey="'rows'"
      :fetchapi="fetchApi"
      :params="fetchParams"
    >
      <!-- :onSyncTableData="handleTableDataChange" -->
      <!-- :dataSource="dataSource"
      :isMemoryPagination="true" -->
      <template slot="controls">
        <!-- <el-button @click="openPayments" type="primary" >收 款</el-button> -->
      </template>
    </FilterTable>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { queryPartMonthReport } from '@/api/part/report/partreport';
// import { getStroageList } from '@/api/part/partOutbound/partBreak';
import { getStorageList } from '@/api/part/common';
import moment from 'moment';
export default {
  name: 'partMonth',
  mixins: [dictionary],
  data() {
    return {
      partMonthFilterList: this.createPartFilterList(),
      columns: this.createPartColumns(),
      fetchParams: { xhrAbort: true },
      fetchApi: queryPartMonthReport,
      onChangeColumn: [],
      dataSource: []
    };
  },
  computed: {
    ptype() {
      return this.$route.meta.ptype;
    } // 模块区分
  },
  mounted() {
    // this.BaseTable = this.$refs.partTable.$refs.pageTable;
    // this.initSelect();
  },
  methods: {
    exportFun(row) {
      for (const key in row) {
        switch (key) {
          case 'OPEN_PRICE':
            if (row[key]) {
              row[key] = Number(row[key]).toFixed(2);
            }
            break;
          case 'OPEN_AMOUNT':
            if (row[key]) {
              row[key] = Number(row[key]).toFixed(2);
            }
            break;
          case 'IN_QUANTITY':
            if (row[key]) {
              row[key] = Number(row[key]).toFixed(2);
            }
            break;
          case 'OUT_AMOUNT':
            if (row[key]) {
              row[key] = Number(row[key]).toFixed(2);
            }
            break;
          case 'INVENTORY_AMOUNT':
            if (row[key]) {
              row[key] = Number(row[key]).toFixed(2);
            }
            break;
          case 'CLOSE_PRICE':
            if (row[key]) {
              row[key] = Number(row[key]).toFixed(2);
            }
            break;
          case 'CLOSE_AMOUNT':
            if (row[key]) {
              row[key] = Number(row[key]).toFixed(2);
            }
            break;
          default:
            break;
        }
      }
    },
    async initSelect() {
      // this.columns.find(x => x.dataIndex === 'STORAGE_CODE').editItems = res.data.map(x => ({ text: x.STORAGE_NAME, value: x.STORAGE_CODE + '' }));
    },
    // 点击事件回调
    clickHandle(val) {
      if (val == '1002') {
        let temp = this.partMonthFilterList.findIndex(item => item.fieldName == 'yearMonth');
        this.partMonthFilterList[temp] = {
          type: 'RANGE_DATE',
          label: '日期',
          fieldName: 'dateStart|dateEnd',
          initialValue: [
            moment()
              .startOf('month')
              .format('YYYY-MM-DD'),
            moment(new Date()).format('YYYY-MM-DD HH:mm:ss')
          ],
          rules: [{ required: true, message: '日期不可以为空', trigger: 'change' }]
        };
      } else {
        let temp = this.partMonthFilterList.findIndex(item => item.fieldName == 'dateStart|dateEnd');
        this.partMonthFilterList[temp] = {
          type: 'DATE',
          dateType: 'month',
          label: '年月',
          fieldName: 'yearMonth',
          // style: { minWidth: '100px' },
          initialValue: moment()
            .startOf('month')
            .format('YYYY-MM'),
          rules: [{ required: true, message: '年月不可以为空', trigger: 'change' }]
        };
      }
      this.partMonthFilterList = [...this.partMonthFilterList];
      // console.log(this.partMonthFilterList, 333);
      // console.log(222);
    },
    // 替换列
    replace() {},
    createPartFilterList() {
      return [
        {
          type: 'SELECT',
          label: '仓库',
          fieldName: 'STORAGE_CODE',
          placeholder: '请选择...',
          // rules: [{ required: true, trigger: 'blur', message: '故障类别不能为空' }],
          request: {
            fetchApi: getStorageList,
            params: {},
            valueKey: 'STORAGE_CODE',
            textKey: 'STORAGE_NAME'
          }
        },
        {
          type: 'DATE',
          dateType: 'month',
          label: '年月',
          fieldName: 'yearMonth',
          initialValue: moment()
            .startOf('month')
            .format('YYYY-MM'),
          // style: { minWidth: '100px' },
          rules: [{ required: true, message: '年月不可以为空', trigger: 'change' }]
        },
        {
          type: 'RADIO',
          label: '',
          fieldName: 'status',
          initialValue: '1001',
          itemList: [
            { text: '按年月', value: '1001' },
            { text: '按日期', value: '1002' }
          ],
          selfCols: 1.5,
          change: props => {
            this.clickHandle(props);
          }
        }
      ];
    },

    createPartColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          },
          sorter: true
        },
        {
          title: '报表年份',
          dataIndex: 'REPORT_YEAR',
          sorter: true,
          filter: true,
          filterType: 'number'
        },
        {
          title: '报表月份',
          dataIndex: 'REPORT_MONTH',
          sorter: true,
          filter: true,
          filterType: 'number'
        },

        {
          title: '仓库',
          dataIndex: 'STORAGE_NAME',
          filter: true,
          filterType: 'input',
          sorter: true
        },
        {
          title: '配件代码',
          dataIndex: 'PART_NO',
          filter: true,
          filterType: 'input',
          sorter: true,
          showOverflowTooltip: true
        },
        {
          title: '配件名称',
          dataIndex: 'PART_NAME',
          // width: 170,
          showOverflowTooltip: true,
          filter: true,
          filterType: 'input',
          sorter: true
        },
        {
          title: '九大类',
          dataIndex: 'PART_MAIN_TYPE',
          render: props => {
            return <span>{this.createDictText(props.row.PART_MAIN_TYPE, '8094')}</span>;
          },
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList(8094),
          dictItems: this.createDictList(8094)
        },
        {
          title: '期初数量',
          dataIndex: 'OPEN_QUANTITY',
          sorter: true,
          filter: true,
          filterType: 'number',
          precision: 2,
          summation: true,
          summationDataIndex: 'TOTAL_OPEN_QUANTITY'
        },
        {
          title: '期初单价',
          dataIndex: 'OPEN_PRICE',
          sorter: true,
          precision: 2,
          filter: true,
          filterType: 'number'
          // summation: true,
          // summationDataIndex: 'TOTAL_OPEN_PRICE'
        },
        {
          title: '期初金额',
          dataIndex: 'OPEN_AMOUNT',
          sorter: true,
          precision: 2,
          filter: true,
          filterType: 'number',
          summation: true,
          summationDataIndex: 'TOTAL_OPEN_AMOUNT'
        },
        {
          title: '入库数量',
          dataIndex: 'IN_QUANTITY',
          sorter: true,
          precision: 2,
          filter: true,
          filterType: 'number',
          summation: true,
          summationDataIndex: 'TOTAL_IN_QUANTITY'
        },
        {
          title: '入库金额',
          dataIndex: 'STOCK_IN_AMOUNT',
          sorter: true,
          precision: 2,
          filter: true,
          filterType: 'number',
          summation: true,
          summationDataIndex: 'TOTAL_STOCK_IN_AMOUNT'
        },
        {
          title: '出库数量',
          dataIndex: 'OUT_QUANTITY',
          sorter: true,
          filter: true,
          filterType: 'number',
          precision: 2,
          summation: true,
          summationDataIndex: 'TOTAL_OUT_QUANTITY'
        },
        {
          title: '出库金额',
          dataIndex: 'OUT_AMOUNT',
          sorter: true,
          precision: 2,
          filter: true,
          filterType: 'number',
          summation: true,
          summationDataIndex: 'TOTAL_OUT_AMOUNT'
        },
        {
          title: '盈亏数量',
          dataIndex: 'INVENTORY_QUANTITY',
          sorter: true,
          filter: true,
          filterType: 'number',
          precision: 2,
          summation: true,
          summationDataIndex: 'TOTAL_INVENTORY_QUANTITY'
        },
        {
          title: '盈亏金额',
          dataIndex: 'INVENTORY_AMOUNT',
          sorter: true,
          precision: 2,
          filter: true,
          filterType: 'number',
          summation: true,
          summationDataIndex: 'TOTAL_INVENTORY_AMOUNT'
        },
        {
          title: '期末数量',
          dataIndex: 'CLOSE_QUANTITY',
          sorter: true,
          filter: true,
          filterType: 'number',
          precision: 2,
          summation: true,
          summationDataIndex: 'TOTAL_CLOSE_QUANTITY'
        },
        {
          title: '期末单价',
          dataIndex: 'CLOSE_PRICE',
          sorter: true,
          precision: 2,
          filter: true,
          filterType: 'number'
          // summation: true,
          // summationDataIndex: 'TOTAL_CLOSE_PRICE'
        },
        {
          title: '期末金额',
          dataIndex: 'CLOSE_AMOUNT',
          sorter: true,
          precision: 2,
          filter: true,
          filterType: 'number',
          summation: true,
          summationDataIndex: 'TOTAL_CLOSE_AMOUNT'
        }
      ];
    },
    // 监听table数据变化
    // handleTableDataChange(e) {
    //   e.map(item => {
    //     item.CLOSE_AMOUNT = item.CLOSE_PRICE * item.CLOSE_QUANTITY;
    //   });
    // },
    async changeHandle(val) {
      // this.fetchParams.xhrAbort = true;
      console.log(val);
      if (val.status == 1002) {
        val.dateStart = val.dateStart ? val.dateStart.split(' ')[0] + ' 00:00:00' : '';
        val.dateEnd = val.dateEnd ? val.dateEnd.split(' ')[0] + ' 23:59:59' : '';
        // 增加时间限制
        if (val.dateStart || val.dateEnd) {
          let diffMonth = moment(val.dateEnd).diff(moment(val.dateStart), 'months');
          if (diffMonth > 5) return this.$notify.warning({ title: '提示信息', message: '起止日期跨度不能超过半年' });
        }
      }
      this.fetchParams.xhrAbort = true;
      this.fetchParams = Object.assign({ xhrAbort: false }, val, { ptype: this.ptype ? this.ptype : null });
      // this.fetchParams = Object.assign(val, { ptype: this.ptype ? this.ptype : null });
      // try {
      //   this.$refs.table.START_LOADING();
      //   this.dataSource = [];
      //   let res = await queryPartMonthReport(this.fetchParams);
      //   this.dataSource = res.data;
      //   this.$refs.table.STOP_LOADING();
      // } catch (error) {
      //   console.log(error);
      // } finally {
      //   this.$refs.table.STOP_LOADING();
      // }
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.$refs.table.$refs.pageTable.EXECUTE_RESET_HEIGHT();
      });
    }
  }
};
</script>

<style scoped lang="less"></style>
